<?php
/**
 * 
 * @author Youssef Driouach
 *
 */
class Application_Model_ClothingMapper{

	protected $_dbTable;

	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Clothing');
		}
		return $this->_dbTable;
	}
	
	public function save(Application_Model_Clothing $clothing){
		
		
		$productMapper = new Application_Model_ProductMapper();
		$row = $productMapper->save($clothing);
		//TODO set the object save function to return the id of the object saved
		//$where = array('model = ?' => (string)$product->getModel());
		$clothing->setProductId($row);
		
		$data = array(
				'id_product' 	=> $clothing->getProductId(),
				'type'			=> $clothing->getType(),
				'matter'		=> $clothing->getMatter(),
				'for'			=> $clothing->getFor(),
				'size'			=> $clothing->getSize(),
		);
		
		if (null === ($id = $clothing->getclothingId())) {
			unset($data['id_clothing']);
			return $this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id_clothing = ?' => $id));
			return $id;
		}
	}
	
	public function find($id, Application_Model_Clothing $clothing)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$clothing->setClothingId($row->id_clothing);
		$clothing->setProductId($row->id_product);
		$clothing->setType($row->type);
		$clothing->setMatter($row->matter);
		$clothing->setFor($row->for);
		$clothing->setSize($row->size);
		
		$productMapper = new Application_Model_ProductMapper();
		$clothing = $productMapper->find($clothing->getProductId(), $clothing);
	
		return $clothing;
	}
	
	public function delete($id)
	{
	
		$clothing= new Application_Model_Clothing();
		$productMapper = new Application_Model_ProductMapper();
		$clothing = $this->find($id, new Application_Model_Clothing());
	
		$result = $this->getDbTable()->find($id);
		if (0 != count($result)) {
			$this->getDbTable()->delete(array('id_clothing = ?' => $id));
		}
	
		$productMapper->delete($clothing->getProductId());
	
	
	}
	
	public function indexBuilder(Zend_Search_Lucene_Document $doc, Application_Model_Clothing $clothing)
	{
		$productMapper = new Application_Model_ProductMapper();
		$requestBuilder = new Application_Model_Builder_Request();
		$doc->addField(Zend_Search_Lucene_Field::Text('type', $clothing->getType(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('matter',$clothing->getMatter(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('for',$clothing->getFor(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('size',$clothing->getSize(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('objectType', 'clothing', 'utf-8'));
		$doc = $productMapper->indexBuilder($doc, $clothing);
	
		return $doc;
	}
	
}